package com.goldgov.jsondoc.doc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.goldgov.jsondoc.revsoler.JsonInfo;
import com.goldgov.jsondoc.revsoler.OperateInfo;
import com.goldgov.jsondoc.revsoler.ParametersInfo;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.rtf.RtfWriter2;

public class GenerateWordByIText {

	@SuppressWarnings("rawtypes")
	public static void generateDocument(OutputStream out, JsonInfo jsonInfo) throws Exception {

		Document document = new Document(PageSize.A4);
		RtfWriter2.getInstance(document, out);
		document.open();

		Paragraph context = new Paragraph();

		StyleSheet ss = new StyleSheet();
		String html = buildHtmlByJson(jsonInfo);
		ArrayList htmlList = HTMLWorker.parseToList(new StringReader(html), ss);
		for (int i = 0; i < htmlList.size(); i++) {
			com.lowagie.text.Element e = (com.lowagie.text.Element) htmlList.get(i);
			context.add(e);
		}
		document.add(context);
		document.close();
	}

	private static String buildHtmlByJson(JsonInfo jsonInfo) {
		String description = jsonInfo.getInfo().getDescription();
		
		StringBuffer html = new StringBuffer();
		LinkedHashMap<String, LinkedHashMap<String, OperateInfo>> paths = jsonInfo.getPaths();
		Set<String> keySet = paths.keySet();
		for (String path : keySet) {
			LinkedHashMap<String, OperateInfo> requestTypeMap = paths.get(path);
			Set<String> requestTypeSet = requestTypeMap.keySet();
			for (String requestType : requestTypeSet) {
				OperateInfo operateInfo = requestTypeMap.get(requestType);
				DocBean docBean = getBean(path, requestType, operateInfo);
				html.append(buildHtml(docBean));
			}
		}
		return "<h2>"+ description +"</h2>" + html.toString();
	}
	
	private static DocBean getBean(String path,String requestType,OperateInfo operateInfo) {
		DocBean bean = new DocBean();
		bean.setInterfaceDesc(operateInfo.getSummary());
		bean.setRequestType(requestType);
		
		String paramStr = "";
        List<ParametersInfo> parameters = operateInfo.getParameters();
        if(parameters != null) {
        	for(ParametersInfo pinfo : parameters) {
        		paramStr += pinfo.getName() + " " + (pinfo.getDescription() == null ? "" : pinfo.getDescription()) + " " + pinfo.getType() + "<br/>";
        	}
        }
		bean.setParamDefine(paramStr);
		bean.setInputUrl(path + "/" + operateInfo.getOperationId());
		
		JSONObject parseObject = JSON.parseObject(operateInfo.getResponses(), Feature.AutoCloseSource);
		bean.setReturnJson("responses : " + new JsonFormat("<br/>","&nbsp;&nbsp;").format(parseObject, 2,false));
		
		return bean;
	}
	
	private static String buildHtml(DocBean docBean) {
		String table = "<table border=1>"
				+ "<tr><td>接口描述</td><td colspan=2>" + docBean.getInterfaceDesc() +"</td></tr>"
				+ "<tr><td>请求方式</td><td colspan=2>" + docBean.getRequestType() +"</td></tr>"
				+ "<tr><td>参数定义</td><td colspan=2>" + docBean.getParamDefine() +"</td></tr>"
				+ "<tr><td>输入URL</td><td colspan=2>" + docBean.getInputUrl() +"</td></tr>"
				+ "<tr><td>返回值</td><td colspan=2>" + docBean.getReturnJson() +"</td></tr>"
				+ "</table>";
		return table;
	}

	public static void main(String[] args) throws Exception {
		File file = new File("C:\\Users\\leemonk\\Desktop\\test.doc");
		if (!file.exists()) {
			file.createNewFile();
		}
		GenerateWordByIText.generateDocument(new FileOutputStream(file), null);
	}
}
